package com.ruoyi.cms.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.cms.domain.*;
import com.ruoyi.cms.domain.vo.CmsSiteCaseVo;
import com.ruoyi.cms.domain.vo.CmsSiteModule4Vo;
import com.ruoyi.cms.service.ICmsSiteBanneService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.authentication.preauth.j2ee.J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.CmsSiteCaseMapper;
import com.ruoyi.cms.service.ICmsSiteCaseService;

/**
 * 案例模块信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-31
 */
@Service
public class CmsSiteCaseServiceImpl implements ICmsSiteCaseService
{
    @Autowired(required = false)
    private CmsSiteCaseMapper cmsSiteCaseMapper;

    @Autowired
    private ICmsSiteBanneService cmsSiteBanneService;

    /**
     * 查询案例模块信息
     *
     * @param id 案例模块信息主键
     * @return 案例模块信息
     */
    @Override
    public CmsSiteCase selectCmsSiteCaseById(Long id)
    {
        return cmsSiteCaseMapper.selectCmsSiteCaseById(id);
    }

    /**
     * 查询案例模块信息列表
     *
     * @param cmsSiteCase 案例模块信息
     * @return 案例模块信息
     */
    @Override
    public List<CmsSiteCase> selectCmsSiteCaseList(CmsSiteCase cmsSiteCase)
    {
        return cmsSiteCaseMapper.selectCmsSiteCaseList(cmsSiteCase);
    }

    /**
     * 新增案例模块信息
     *
     * @param cmsSiteCase 案例模块信息
     * @return 结果
     */
    @Override
    public int insertCmsSiteCase(CmsSiteCase cmsSiteCase)
    {
        return cmsSiteCaseMapper.insertCmsSiteCase(cmsSiteCase);
    }

    /**
     * 修改案例模块信息
     *
     * @param cmsSiteCase 案例模块信息
     * @return 结果
     */
    @Override
    public int updateCmsSiteCase(CmsSiteCase cmsSiteCase)
    {
        return cmsSiteCaseMapper.updateCmsSiteCase(cmsSiteCase);
    }

    /**
     * 批量删除案例模块信息
     *
     * @param ids 需要删除的案例模块信息主键
     * @return 结果
     */
    @Override
    public int deleteCmsSiteCaseByIds(Long[] ids)
    {
        return cmsSiteCaseMapper.deleteCmsSiteCaseByIds(ids);
    }

    /**
     * 删除案例模块信息信息
     *
     * @param id 案例模块信息主键
     * @return 结果
     */
    @Override
    public int deleteCmsSiteCaseById(Long id)
    {
        return cmsSiteCaseMapper.deleteCmsSiteCaseById(id);
    }
    //同步分页
    /*@Override
    public List<CmsSiteCaseVo> getAllInfo(String page) {
        List<CmsSiteCase> cmsSiteCases = this.selectCmsSiteCaseList(null);
        List<CmsSiteBanne> cmsSiteBannes = cmsSiteBanneService.selectCmsSiteBanneList(null);
        List<CmsSiteBanne> pageData = cmsSiteBanneService.selectCmsSiteBanneListPage("3",Integer.parseInt(page) * 6 - 6, 6);
        List<CmsSiteBanne> pageData2 = cmsSiteBanneService.selectCmsSiteBanneListPage("4",Integer.parseInt(page) * 6 - 6, 6);

        //封装数据
        List<CmsSiteCaseVo> collect = cmsSiteCases.stream().map(module -> {
            CmsSiteCaseVo cmsSiteWebsitVo = new CmsSiteCaseVo();
            BeanUtils.copyProperties(module, cmsSiteWebsitVo);
            List<CmsSiteBanne> cmsSiteBanners1 = new ArrayList<>();
            cmsSiteBannes.stream().forEach(banner -> {
                if (module.getId().equals(banner.getCaseId())) {
                    cmsSiteBanners1.add(banner);
                }
            });
            //放之前排序
            cmsSiteBanners1.sort(Comparator.comparing(CmsSiteBanne::getSort));

            if(cmsSiteWebsitVo.getId().toString().equals("3")){
                cmsSiteWebsitVo.setCmsSiteBannes(pageData);
            }else if(cmsSiteWebsitVo.getId().toString().equals("4")){
                cmsSiteWebsitVo.setCmsSiteBannes(pageData2);
            }else{
                cmsSiteWebsitVo.setCmsSiteBannes(cmsSiteBanners1);
            }

            return cmsSiteWebsitVo;
        }).collect(Collectors.toList());

        //返回前排序
        collect.sort(Comparator.comparing(CmsSiteCaseVo::getSort));



        return collect;
    }*/


    @Override
    public List<CmsSiteCaseVo> getAllInfo(CmsSiteBanne param) {
        List<CmsSiteBanne> cmsSiteBannes = cmsSiteBanneService.selectCmsSiteBanneList(param);
        List<CmsSiteCase> cmsSiteCases = this.selectCmsSiteCaseList(null);


        //封装数据
        List<CmsSiteCaseVo> collect = cmsSiteCases.stream().map(module -> {
            CmsSiteCaseVo cmsSiteWebsitVo = new CmsSiteCaseVo();
            BeanUtils.copyProperties(module, cmsSiteWebsitVo);
            List<CmsSiteBanne> cmsSiteBanners1 = new ArrayList<>();
            cmsSiteBannes.stream().forEach(banner -> {
                if (module.getId().equals(banner.getCaseId())) {
                    cmsSiteBanners1.add(banner);
                }
            });
            //放之前排序
            cmsSiteBanners1.sort(Comparator.comparing(CmsSiteBanne::getSort));
            cmsSiteWebsitVo.setCmsSiteBannes(cmsSiteBanners1);
            return cmsSiteWebsitVo;
        }).collect(Collectors.toList());

        //返回前排序
        collect.sort(Comparator.comparing(CmsSiteCaseVo::getSort));

        return collect;
    }
}
